package com.rtx.wms.std.validation.transfer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.common.bean.Codelkup;
import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.erpconfig.bean.RtxErpConfig;
import com.rtx.wms.std.inv.bean.Lotattribute;
import com.rtx.wms.std.inv.bean.Lotxlocxid;
import com.rtx.wms.std.inv.service.LotattributeService;
import com.rtx.wms.std.inv.service.LotxlocxidService;
import com.rtx.wms.std.transer.bean.ApiTransfer;
import com.rtx.wms.std.transer.bean.ApiTransferdetail;
import com.rtx.wms.std.validation.BaseValidation;

/**
 * 出库单验证
 */
@Component
public class TransferValidation extends BaseValidation {
	
	@Autowired
	LotattributeService lotattributeService;
	
	@Autowired
	LotxlocxidService lotxlocxidService;

	/**
	 * 校验企业级数据
	 */
	@DataSourceAnnotation
	public void  checkEnterpriseConfig(String datasourekey,ApiTransfer o) {
		Set<String> whseidSet = new HashSet<>();
		
		Set<String> targetWerks = new HashSet<>();
		Set<String> targetLgort = new HashSet<>();
		
		//校验单据类型转换
		/*Codelkup typeConfig = validationCodelkup(WmsConstant.ENTERPRISE,o.getType(),"TRANTYPE",true);
		o.setTypeConfig(typeConfig);*/
		
		for(ApiTransferdetail od:o.getDetails()) {
			List<RtxErpConfig> configList =	validationErpConfig(WmsConstant.ENTERPRISE, od.getFromlottable01(), od.getFromlottable02());
			od.setWhseid(configList.get(0).getWmwhse());
			whseidSet.add(configList.get(0).getWmwhse());
			o.setWhseid(configList.get(0).getWmwhse());
			
			targetWerks.add(od.getLottable01());
			targetLgort.add(od.getLottable02());
			
			//校验下发sku
			validationSku(WmsConstant.ENTERPRISE, o.getStorerkey(), od.getFromsku(), true);
			
			validationSku(WmsConstant.ENTERPRISE, o.getStorerkey(), od.getTosku(), true);

		}
		
		
		
		if(whseidSet.size()>1) {
			throw new RuntimeException("明细中-原工厂-fromlottable01-原库存地-fromlottable02-在工厂库存地中-只能wms一个仓库");
		}
		
		if(targetWerks.size()>1) {
			throw new RuntimeException("明细中目标工厂-lottable01-只能存在一个");
		}
		
		if(targetLgort.size()>1) {
			throw new RuntimeException("明细中目标库存地点-lottable02-只能存在一个");
		}
	}
	
	@DataSourceAnnotation
	public void check(String datasourckey,ApiTransfer o) {
		for(ApiTransferdetail td:o.getDetails()) {
			
			Date fromlottable04 = null;
			Date fromlottable05 = null;
			try {
				SimpleDateFormat df =	new SimpleDateFormat("yyyy-MM-dd");
				fromlottable04 = df.parse(td.getFromlottable04());
				fromlottable05 = df.parse(td.getFromlottable05());
			} catch (Exception e) {
			}
			
			LambdaQueryWrapper<Lotattribute> query = Wrappers.lambdaQuery();
			query.eq(Lotattribute::getStorerkey, o.getStorerkey());
			query.eq(Lotattribute::getSku, td.getFromsku());
			query.eq(Lotattribute::getLottable01, td.getFromlottable01());
			query.eq(Lotattribute::getLottable02, td.getFromlottable02());
			query.eq(Lotattribute::getLottable03, td.getFromlottable03());
			if (fromlottable04 != null) {
				query.eq(Lotattribute::getLottable04, fromlottable04);
			}else{
				query.isNull(Lotattribute::getLottable04);
			}
			if (fromlottable05 != null) {
				query.eq(Lotattribute::getLottable05, fromlottable05);
			}else{
				query.isNull(Lotattribute::getLottable05);
			}
			query.eq(Lotattribute::getLottable06, td.getFromlottable06());
			query.eq(Lotattribute::getLottable07, td.getFromlottable07());
			query.eq(Lotattribute::getLottable08, td.getFromlottable08());
			query.eq(Lotattribute::getLottable09, td.getFromlottable09());
			List<Lotattribute> list = lotattributeService.queryByCondition(o.getWhseid(), query);
			if(CollectionUtils.isEmpty(list)) {
				throw new RuntimeException("转移的物料-"+td.getFromsku()+"-工厂-"+td.getLottable01()+"-库存地-"+td.getLottable02()+"-数量不是整批数量");
			}
			LambdaQueryWrapper<Lotxlocxid> lquery = Wrappers.lambdaQuery();
			lquery.eq(Lotxlocxid::getLot, list.get(0).getLot());
			lquery.eq(Lotxlocxid::getSku, list.get(0).getSku());
			lquery.eq(Lotxlocxid::getStorerkey, o.getStorerkey());
			List<Lotxlocxid> list2 = lotxlocxidService.queryByCondition(datasourckey, lquery);
			Double qty = list2.stream().mapToDouble(temp->Double.parseDouble(temp.getQty())).sum();
			
			if(Double.compare(qty, Double.parseDouble(td.getQty()))!=0) {
				throw new RuntimeException("转移的物料-"+td.getFromsku()+"-工厂-"+td.getLottable01()+"-库存地-"+td.getLottable02()+"-数量不是整批数量");
			};
			
			td.setLotxlocxid(list2.get(0));
		}

	}
}
